#include "Stack.h"

void STInit(ST* pst)
{
	assert(pst);
	pst->capacity = 0;
	pst->top = 0;
	pst->data = NULL;
}

void STPush(ST* pst, ElemType x)
{
	assert(pst);
	if (pst->top == pst->capacity)
	{
		int newCap = pst->capacity == 0 ? 4 : pst->capacity * 2;
	    ElemType* tmp = (ElemType*)realloc(pst->data, newCap*sizeof(ElemType));
		if (tmp == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		pst->data = tmp;
		pst->capacity = newCap;
	}
	pst->data[pst->top] = x;
	pst->top++;
}

void STPop(ST* pst)
{
	assert(pst);
	if (pst->top == 0)
		return;
	
	pst->top--;
}

ElemType STTop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);
	return pst->data[pst->top - 1];
}

void STDestroy(ST* pst)
{
	assert(pst);
	free(pst->data);
	pst->capacity = pst->top = 0;
}


bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == 0;
}

int STSize(ST* pst)
{
	assert(pst);
	return pst->top;
}
